package JFrame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

import static Tools.Tool.*;

public class JFrame0 extends JFrame {
    JButton Buttonk = new JButton("开始游戏");
    BufferedImage bufferedImage;// 缓冲图像
    GameJPanel P1;

    public JFrame0() {
        this.setDefaultCloseOperation(JFrame0.EXIT_ON_CLOSE);
        this.setTitle("小游戏1");
        this.setBounds(400, 0, 1080, 1080);
        this.getContentPane().setBackground(Color.black);
        this.setLayout(new FlowLayout());
        Buttonk.setPreferredSize(new Dimension(100, 50));
        Buttonk.setBackground(Color.ORANGE);
        Buttonk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initGame();
            }
        });
        this.add(Buttonk, BorderLayout.NORTH);
        this.setVisible(true);
        revalidate();
    }

    // 初始化游戏
    public void initGame() {
        getContentPane().removeAll();
        JPanel pan0=new JPanel();
        JButton shuangB=new JButton("双人游戏");
        shuangB.setPreferredSize(new Dimension(200, 80));
        shuangB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getContentPane().removeAll();
                P1=new GameJPanel(2);
                P1.setPreferredSize(new Dimension(1080, 1180));
                add(P1);
                P1.requestFocusInWindow();
                repaint();
                validate();
            }
        });
        pan0.setLayout(new GridLayout(5,1));
        pan0.add(shuangB);
        add(pan0,BorderLayout.CENTER);
        repaint();
        validate();

    }
    // 重写绘制方法
    @Override
    public void paint(Graphics g){
        try {
            bufferedImage=ImageIO.read(new FileInputStream(imgpath0));// 读取图像文件到缓冲图像
            g.drawImage(bufferedImage,0,0,null);// 在窗口中绘制缓冲图像
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

